package com.fanqiechaodan.flyweight;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author fanqiechaodan
 * @Classname ProductFactory
 * @Description 享元工厂
 */
public class ProductFactory {

    private static Map<String, Product> productMap = new HashMap<>();

    public static Product getProduct(String name, double price, int stock) {
        String key = name + "_" + price;
        Product res = productMap.get(key);
        if (Objects.isNull(res)) {
            // 如果缓存池内不存在,就创建新对象放置到缓存池
            res = new ConcreteProduct(name, price, stock);
            productMap.put(key, res);
        }
        return res;
    }
}
